package com.linc.skill.thread;

import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.android.widget.demo.R;

public class ThreadActivity extends AppCompatActivity {
    private static final String TAG = "ThreadActivity";
    private NumberCountThread mThread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thread);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
    }

    public void onTestClick(View view) {
        mThread = new NumberCountThread();
        mThread.start();
    }

    public void onRunnableClick(View view) {
        startActivity(new Intent(this, RunnableActivity.class));
    }

    public void onStopClick(View view) {
        mThread.interrupt();
    }

    private class NumberCountThread extends Thread {
        public void run () {
            for (int i = 0;i<1000*1000;++i) {
                Log.d(TAG,"count: "+i);
                if(mThread.isInterrupted()) {
                    Log.d(TAG,"interrupted, return!");
                    return;
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
